package com.boxin.ims.modules.wechat.busi;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.entity.screen.BlessWords;
import com.boxin.ims.modules.wechat.entity.screen.WeddingScreen;
import com.boxin.ims.modules.wechat.service.screen.BlessWordsService;
import com.boxin.ims.modules.wechat.service.screen.ScreenConfigService;
import com.boxin.ims.modules.wechat.service.screen.WeddingScreenService;
import com.boxin.ims.modules.wechat.utils.ScreenUtils;

/**
 * @author Jakemanse
 * @date 2013-11-1
 * @function <p>微信墙服务处理类 </p>
 */
@Service
public class ScreenProcessor extends BusinessProcessor {
	
	@Autowired
	private WeddingScreenService weddingScreenService;
	@Autowired
	private ScreenConfigService screenConfigService;
	
	@Autowired
	private BlessWordsService blessWordsService;
	
	public ScreenProcessor(){
		super();
	}

	/**
	 * 关键词格式如:
	 * 祝福张三新婚快乐，早生贵子
	 */
	@Override
	public ProcessMessage process() {
		
		//1.关键词过虑
		//2.关键词定义 
		
		WechatMember member = memberCheckAndReply();
		
		if(member!=null){
			String reply = "祝福发送成功,谢谢您的祝福!";
			List<WeddingScreen>  screens = 	weddingScreenService.findByUserIdAndDate(user.getId(), DateUtils.getTodayString());
			if(screens!=null && !screens.isEmpty()){
				for(WeddingScreen screen : screens){
					String keywords = textMessage.getContent();
					//过滤关键词
					if(isFiltered(keywords,screen.getFilter())){
						if(keywords.contains(screen.getBridegroomName()) || keywords.contains(screen.getBrideName())){
							String brideroomName = screen.getBridegroomName();
							String brideName = screen.getBrideName();
							String blessWords = "";
							blessWords = keywords.replaceAll("祝福", "");
							blessWords = blessWords.replaceAll(brideroomName, "");
							blessWords = blessWords.replaceAll(brideName, "");
							
							BlessWords bsds = new BlessWords();
							bsds.setMember(member);
							bsds.setWords(blessWords);
							bsds.setScreen(screen);
							bsds.setName(member.getName());
							bsds.setUser(user);
							blessWordsService.save(bsds);
							
							//返回给祝福者信息
							writeToUser(reply);
							
							ScreenUtils.push(bsds);	//添加到缓存
						}
						
					}else{
						reply = "请不要发送恶意消息,谢谢！";
						writeToUser(reply);
					}
				}
				
				return null;
				
			}else{
				//未找到祝福的对象的人
				reply = "新人祝福活动还没有启动,谢谢您对新人的祝福!";
				writeToUser(reply);
			}
			
		}
		
		return null;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-2  下午2:35:22
	 * @function <p> 过滤脏词是否通过 </p>
	 * @param keywords
	 * @param filter
	 * @return
	 */
	public boolean isFiltered(String keywords,String filter){
		keywords = keywords.replaceAll(" ", "");
		String farray[] = filter.split(",");
		for(String str : farray){
			if(keywords.contains(str)){
				return false;
			}
		}
		
		return true;
		
	}
	

	@Override
	public boolean isNeedBeMember() {
		return false;
	}

}
